﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Xys.Remp.ServiceModel;
using Xys.Remp.ServiceModel.Runtime;
using System.IO;
using System.Text;
using Service.Management.Web.Models;

namespace Service.Management.Web.Controllers
{
    public class PackageController : Controller
    {
        public ActionResult Index()
        {
            return View();
        }

        private IServiceConsole GetService() {
            //return ClientFactory.Current.GetClientProxy<IServiceConsole>("IServiceConsole");
            var factory = new System.ServiceModel.ChannelFactory<IServiceConsole>("IServiceConsole");
            return factory.CreateChannel();
        }

        public string Services(int id)
        {
            if (id == 0)
            {
                var service = GetService();
                var lst = service.GetServicePackages().Select(c => new TreeNode() { 
                    id = c.Id,
                    text = c.Name,
                    isPackage = true,
                    hasChildren = true }).ToArray();
                var sb = new StringBuilder();
                var sr = new StringWriter(sb);
                new Newtonsoft.Json.JsonSerializer().Serialize(sr, lst);
                return sb.ToString();
            }
            else
                return ServiceList(id);
        }

        public string ServiceList(int packageId)
        { 
            var service = GetService();
            var lst = service.GetServiceInfo(packageId).Select( c=> new TreeNode()
                {
                    id= packageId,
                    text = c.FullName,
                    hasChildren = false,
                    isPackage = false,
                    status = c.State.ToString()
                }).ToList();
            var sb = new StringBuilder();
            var sr = new StringWriter(sb);
            new Newtonsoft.Json.JsonSerializer().Serialize(sr, lst);
            return sb.ToString();
        }

        public string StopService(int packageId, string serviceFullName)
        {
            var service = GetService();
            if (!string.IsNullOrEmpty(serviceFullName))
                service.StopService(packageId, serviceFullName);
            else
                service.StopService(packageId);

            return string.Empty;
        }

        public string StartService(int packageId, string serviceFullName)
        {
            var service = GetService();

            if (!string.IsNullOrEmpty(serviceFullName))
                service.StartService(packageId, serviceFullName);
            else
                service.StartService(packageId);

            return string.Empty;

            //var node = new TreeNode() { id = packageId, text = serviceFullName, isPackage = true, status = RunState.Runnable.ToString() };
            //var sb = new StringBuilder();
            //var sr = new StringWriter(sb);
            //new Newtonsoft.Json.JsonSerializer().Serialize(sr, node);
            //return sb.ToString();
        }

        public string InfoService(int packageId, string serviceFullName)
        {
            var service = GetService();

            var lst = service.GetServiceInfo(packageId, serviceFullName).Select(c=>{
                return new TreeNode()
                {
                    id = packageId,
                    text = c.FullName,
                    isPackage = false,
                    status = c.State.ToString()
                };
            });

            var sb = new StringBuilder();
            var sr = new StringWriter(sb);
            new Newtonsoft.Json.JsonSerializer().Serialize(sr, lst);
            return sb.ToString();
        }
    }
}
